$(function () {

    //    查询及分页部分
    let Pointer = 0
    let pager_number = 1
    let data_for_one_num = 10
    let page_data_url = baseURL + '/auth/api/dic/searchDic'
    let authorities = $.session.get('authorities');
    console.log(authorities);

    function append(dicvalue, dicgroup) {
        $("#data-list").empty();
        //ajax start
        $.ajax({
            url: page_data_url,
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                //offset:起始位置 = 指针
                //limit:获取条数 =默认为10
                limit: data_for_one_num,
                offset: Pointer,
                dicvalue: dicvalue,
                dicgroup: dicgroup,
            },
            success: function (c) {
                //all_page_num为数据总量
                all_data_num = c.total
                //向上取整 max_page为最大页码
                max_page = Math.ceil(all_data_num / data_for_one_num)
                //if 判断 start
                if (c.code == -3000) {
                    $("#data-list").append(
                        alert("没有找到数据")
                    );
                }
                //for i 循环 start
                for (let i = 0; i < data_for_one_num; i++) {
                    $("#data-list").append(
                        '<tr><td class="uuid">' + c.content[i].uuid + '</td><td class="dicgroup">' + c.content[i].dicgroup + '</td> <td class="dickey">'
                        + c.content[i].dickey + '</td><td class="dicvalue">' + c.content[i].dicvalue + '</td><td class="state">' + c.content[i].state + '</td> <td class="revision">' + c.content[i].revision + '</td>' +
                        '<td><a data-toggle="modal" class="modify-modal" href="#modify">修改 </a><a class="del"> 删除</a></td></tr>'
                    )
                }
            }
        })
    }

//ajax start 获取总量 与 第一页的显示
    $.ajax({
        url: page_data_url,
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            //offset:起始位置 = 指针
            //limit:获取条数 =默认为10
            limit: 0,
            offset: 0,
        },
        success: function (e) {
            //all_page_num为数据总量
            all_data_num = e.total
            //向上取整 max_page为最大页码
            max_page = Math.ceil(all_data_num / data_for_one_num)
            //页面第一次加载时执行append
            append();
        }

    })

    //切换页面方法
    function page_change(to_page) {
        if (to_page <= 0) {
            Pointer = 0
            pager_number = 1
            append()
            return '不能小于或等于0'
        } else if (to_page > 0) {
            if (to_page > max_page) {
                Pointer = (max_page * data_for_one_num) - data_for_one_num
                pager_number = max_page
                append()
                return "超过最大值"
            } else if (to_page <= max_page) {
                Pointer = (to_page * data_for_one_num) - data_for_one_num
                //用户切换了页面 改变了指针位置 执行append方法
                pager_number = to_page
                append()
            }
        }
    }

    //按钮值判断
    $(".previous").click(function () {
        //if 判断 start
        if (pager_number <= 1) {
        } else if (pager_number > 1) {
            page_change(pager_number - 1)
        }
    });
    $(".next").click(function () {
        //if 判断 start
        if (pager_number < max_page) {
            page_change(pager_number + 1)
        } else if (pager_number >= max_page) {

        }
    });

//分页end

//获取学科,难度,年级表
    //ajax start 生成学科列表
    $.ajax({
        url: baseURL + '/auth/api/dic/searchDic',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            dicgroup: '科目',
        },
        success: function (e) {

            //for i 循环 start
            for (let i = 0; i < e.content.length; i++) {
                let v_dic = e.content[i].dickey
                let mark_dicv = e.content[i].dicvalue
                $("#subject").prepend(
                    '<option value=' + v_dic + ' mark=' + mark_dicv + ' id="sub_type' + v_dic + '">' + mark_dicv + '</option>'
                );
            }
        }

    })
    //ajax start 生成年级列表
    $.ajax({
        url: baseURL + '/auth/api/dic/searchDic',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            dicgroup: '年级',
        },
        success: function (e) {
            //for i 循环 start
            for (let i = 0; i < e.content.length; i++) {
                let v_dic = e.content[i].dickey
                let mark_dicv = e.content[i].dicvalue
                $("#grade").prepend(
                    '<option value=' + v_dic + ' mark=' + mark_dicv + ' id="grade_type' + v_dic + '">' + mark_dicv + '</option>'
                );
            }
        }

    })
    //ajax start 生成难度列表
    $.ajax({
        url: baseURL + '/auth/api/dic/searchDic',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            dicgroup: '难易度',
        },
        success: function (e) {
            //for i 循环 start
            for (let i = 0; i < e.content.length; i++) {
                let v_dic = e.content[i].dickey
                let mark_dicv = e.content[i].dicvalue
                $("#difficulty").prepend(
                    '<option value=' + v_dic + ' mark=' + mark_dicv + ' id="grade_type' + v_dic + '">' + mark_dicv + '</option>'
                );
            }
        }

    })
//添加试题部分
//    默认为1
    let subject = 1
    let difficulty = 1
    let grade = 1
    $(".add_title_inf").on("change", "#subject", function () {
        let subject = $(this).val();
        console.log(subject);
    });
    $(".add_title_inf").on("change", "#difficulty", function () {
        let difficulty = $(this).val();
        console.log(difficulty);
    });
    $(".add_title_inf").on("change", "#grade", function () {
        let grade = $(this).val();
        console.log(grade);
    });
    //


    const E = window.wangEditor


    const editor1 = new E('#wangEditor1')
    editor1.config.uploadImgHeaders = {
        'authorities': authorities
    }
    editor1.config.uploadImgServer = baseURL + '/admin/api/uploadFile'
    editor1.config.uploadFileName = 'questionImg'
    editor1.config.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            img_url = result.date[0]
            insertImg(result.date[0]);
        }
    }
    editor1.config.height = 200
    editor1.create()


    const editor2 = new E('#wangEditor2')
    editor2.config.uploadImgHeaders = {
        'authorities': authorities
    }
    editor2.config.uploadImgServer = baseURL + '/admin/api/uploadFile'
    editor2.config.uploadFileName = 'questionImg'
    editor2.config.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            img_url = result.date[0]
            insertImg(result.date[0]);
        }
    }
    editor2.config.height = 200
    editor2.create()

    const editor3 = new E('#wangEditor3')
    editor3.config.uploadImgHeaders = {
        'authorities': authorities
    }
    editor3.config.uploadImgServer = baseURL + '/admin/api/uploadFile'
    editor3.config.uploadFileName = 'questionImg'
    editor3.config.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            img_url = result.date[0]
            insertImg(result.date[0]);
        }
    }
    editor3.config.height = 200
    editor3.create()

    const editor4 = new E('#wangEditor4')
    editor4.config.uploadImgHeaders = {
        'authorities': authorities
    }
    editor4.config.uploadImgServer = baseURL + '/admin/api/uploadFile'
    editor4.config.uploadFileName = 'questionImg'
    editor4.config.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            img_url = result.date[0]
            insertImg(result.date[0]);
        }
    }
    editor4.config.height = 200
    editor4.create()

    const editor5 = new E('#wangEditor5')
    editor5.config.uploadImgHeaders = {
        'authorities': authorities
    }
    editor5.config.uploadImgServer = baseURL + '/admin/api/uploadFile'
    editor5.config.uploadFileName = 'questionImg'
    editor5.config.uploadImgHooks = {
        customInsert: function (insertImg, result, editor) {
            img_url = result.date[0]
            insertImg(result.date[0]);
        }
    }
    editor5.config.height = 200
    editor5.create()

    //上传题目部分
    $("#exam_save").click(function () {
        function if_true(box) {
            //if 判断 start
            if (box == true) {
                return 1
            } else {
                return 0
            }
        }

        let editor_title_txt = editor1.txt.html()
        let editor2txt = editor2.txt.html()
        let editor3txt = editor3.txt.html()
        let editor4txt = editor4.txt.html()
        let editor5txt = editor5.txt.html()
        let option1v = $("#option1").prop('checked')
        let option1 = if_true(option1v)
        let option2v = $("#option2").prop('checked')
        let option2 = if_true(option2v)
        let option3v = $("#option3").prop('checked')
        let option3 = if_true(option3v)
        let option4v = $("#option4").prop('checked')
        let option4 = if_true(option4v)
        //if 判断 start 判断是否为多选题
        let option_type
        if (option1 + option2 + option3 + option4 > 1) {
            option_type = 2
        } else {
            option_type = 1
        }
        var list = [{
            answeritem: editor2txt,
            isRight: option1,
            sn: 1,
        }, {
            answeritem: editor3txt,
            isRight: option2,
            sn: 2,
        }, {
            answeritem: editor4txt,
            isRight: option3,
            sn: 3,
        }, {
            answeritem: editor5txt,
            isRight: option4,
            sn: 4,
        }
        ]
        //ajax start提交题目
        console.log(editor_title_txt);
        console.log(grade);
        console.log(subject);
        console.log(difficulty);
        console.log(list);
        $.ajax({
            url: baseURL + '/admin/api/btquestion/createQuestion',
            type: 'post',
            dataType: 'json',
            headers: {
                'authorities': authorities,
                "ContentType": "application/json"
            },
            data: {
                btAnswerItemListJson: JSON.stringify(list),
                name: editor_title_txt,
                platform: "1",
                attribute: "2",
                grade: grade,
                subject: subject,
                type: option_type,
                difficulty: difficulty,
                point: "0",
            },
            success: function (e) {
                console.log(e);
                console.log(e.msg);
                alert(e.msg)
            }

        })
    })

// jq end
})


